package supercafe.dataprovider;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

public class DataGetter extends AsyncTask<String, String ,Void> {
    private String responsedResult="";
    private Activity ac;
    private FinishGet finish;
    private ProgressDialog m_ProgressDialog;
	private boolean done = false;
    public DataGetter(FinishGet finish,Activity ac){
    	this.ac = ac;
    	this.finish = finish;
    	responsedResult="";
    }
    @Override
    protected Void doInBackground(String... params) {
          InputStream content = null;
          String url = params[0];
          try {
         	if(ac!=null){
       			publishProgress();
         	}
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(new HttpGet(url));
            content = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String temp;
            while((temp = reader.readLine())!=null){
            	responsedResult+=temp;
            }
          } catch (Exception e) {
                Log.e("Network exception","@nhan-DataGetter" );
                e.printStackTrace();
          }
          return(null);
    }
    @Override
    protected void onProgressUpdate(String... values) {
    	m_ProgressDialog = ProgressDialog.show(ac,"Vui lòng chờ...", "Đang lấy dữ liệu...", true);
    }
    @Override
    protected void onPostExecute(Void result) {
    	done  = true;
    	m_ProgressDialog.dismiss();
    	if(finish!=null)
    		finish.doFinishGet();
    }
    public String getResponseResult(){
    	return this.responsedResult;
    }
	public boolean isFinished() {
		// TODO Auto-generated method stub
		return done;
	}
}
